package com.doraemon.controller.user;

import com.doraemon.entity.AddressBook;
import com.doraemon.result.Result;
import com.doraemon.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * c端地址簿管理Controller
 **/
@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址簿相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /***
     * 新增地址
     * ---
     * @param addressBook 地址实体
     * @return: com.doraemon.result.Result
     */
    @PostMapping()
    @ApiOperation("新增地址")
    public Result addAddressBook(@RequestBody AddressBook addressBook) {
        log.info("c端-新增地址：{}", addressBook);

        // 调用Service层 addAddressBook 方法，新增地址
        addressBookService.addAddressBook(addressBook);

        return Result.success();
    }

    /***
     * 根据id删除地址
     * ---
     * @param id 地址id
     * @return: com.doraemon.result.Result
     */
    @DeleteMapping()
    @ApiOperation("根据id删除地址")
    public Result deleteAddressBookById(@RequestParam Long id) {
        log.info("c端-根据id删除地址：{}", id);

        // 调用Service层 deleteAddressBookById 方法，删除地址
        addressBookService.deleteAddressBookById(id);

        return Result.success();
    }

    /***
     * 修改地址
     * ---
     * @param addressBook 地址实体
     * @return: com.doraemon.result.Result
     */
    @PutMapping()
    @ApiOperation("修改地址")
    public Result updateAddressBook(@RequestBody AddressBook addressBook) {
        log.info("c端-修改地址：{}", addressBook.getId());

        // 调用Service层 update 方法，修改地址
        addressBookService.update(addressBook);

        return Result.success();
    }

    /***
     * 设置默认地址
     * ---
     * @param addressBook 地址实体
     * @return: com.doraemon.result.Result
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result updateDefaultAddressBook(@RequestBody AddressBook addressBook) {
        log.info("c端-修改默认地址：{}", addressBook.getId());

        // 调用Service层 setDefaultAddressBook 方法，修改默认地址
        addressBookService.setDefaultAddressBook(addressBook);

        return Result.success();
    }


    /***
     * 查看当前登录用户的所有地址信息
     * ---
     * @param
     * @return: com.doraemon.result.Result<java.util.List < com.doraemon.entity.AddressBook>>
     */
    @GetMapping("/list")
    @ApiOperation("查看当前登录用户的所有地址信息")
    public Result<List<AddressBook>> list() {
        log.info("c端-查看当前登录用户的所有地址信息");

        // 调用Service层 getListAddressBook 方法，获取地址信息集合
        List<AddressBook> addressBooks = addressBookService.getListAddressBook();

        return Result.success(addressBooks);
    }

    /***
     * 查询默认地址
     * ---
     * @param
     * @return: com.doraemon.result.Result<com.doraemon.entity.AddressBook>
     */
    public Result<AddressBook> getDefaultAddressBook() {
        log.info("c端-查询默认地址");

        // 调用Service层 getDefaultAddressBook 方法，获取默认地址实体
        AddressBook addressBook = addressBookService.getDefaultAddressBook();

        return Result.success(addressBook);
    }

    /***
     * 根据id查询地址
     * ---
     * @param id 地址id
     * @return: com.doraemon.result.Result<com.doraemon.entity.AddressBook>
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getAddressBookById(@RequestParam Long id) {
        log.info("c端-根据id查询地址：{}", id);

        // 调用Service层 getAddressBookById 方法，获取地址实体
        AddressBook addressBook = addressBookService.getAddressBookById(id);

        return Result.success(addressBook);
    }
}
